package cn.softgame.cms.common.security;

import org.springframework.security.authentication.encoding.BaseDigestPasswordEncoder;

import cn.softgame.cms.common.util.Constant;

public class TissonDESPasswordEncoder extends BaseDigestPasswordEncoder {
    
	@Override
	public String encodePassword(String rawPass, Object salt) {//合并password和salt数据--->真正的PASSWORD的值=password+'{+salt+'}',如果salt=null的话，则没有'{}'部分内容
		String saltedPass = mergePasswordAndSalt(rawPass, salt, false);
		try {
			DES des = new DES(Constant.SECRET_KEY);
			return des.encrypt(saltedPass);
			
		} catch (Exception ex) {
			throw new IllegalArgumentException("Not support algorithm [ Tisson-Des]:"+ex.getMessage());
		}
	}

	@Override
	public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
        String pass1 = "" + encPass;
        String pass2 = encodePassword(rawPass, salt);
		System.out.println("***********encPass is:" + pass1 +",rawPass is:"+pass2+"***************");
        return PasswordEncoderUtils.equals(pass1,pass2);
	}
	
    public String[] obtainPasswordAndSalt(String password) {//获得password和salt数据[字符串数据组]
        return demergePasswordAndSalt(password);
    }    
}
